package edu.lingnan.rili.controller;

import edu.lingnan.rili.pojo.Administrator;
import edu.lingnan.rili.service.AddawardedService;
import edu.lingnan.rili.utils.DownloadUtils;
import edu.lingnan.rili.utils.Result;
import edu.lingnan.rili.vo.AddAwardedVo;
import edu.lingnan.rili.vo.AwardedVo;
import edu.lingnan.rili.vo.MarksVo;
import edu.lingnan.rili.vo.PagingQueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;

import static edu.lingnan.rili.controller.AdministratorController.isAdminLogin;

/**
 * @author xBaozi
 * @version 1.0
 * @classname AddAwardedController
 * @description 校内比赛加分控制器
 * @date 2022/3/21 9:48
 */
@RestController
@RequestMapping("/add-awarded")
@Api(tags = "校内比赛加分控制器")
public class AddAwardedController {
    @Autowired
    private AddawardedService addawardedService;

    @ApiOperation("分页查询所有已公示的校内比赛加分数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="currentPage",value="当前页数"),
            @ApiImplicitParam(name="pageSize",value="每页条数")
    })
    @GetMapping("/announced-page")
    public Result<PagingQueryResult<AddAwardedVo>> selectAllAnnouncedAndPage(@RequestParam("currentPage") String currentPage,
                                                                             @RequestParam("pageSize") String pageSize,
                                                                             @ApiIgnore HttpSession session) {
        if ("".equals(currentPage) || "".equals(pageSize) || StringUtils.isEmpty(currentPage) ||  StringUtils.isEmpty(pageSize)) {
            return new Result<>(400, "请输入有效数据");
        }
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        PagingQueryResult<AddAwardedVo> addAwardedVoPage = addawardedService.selectByAnnouncedByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        if (addAwardedVoPage.getPageTotal() <= 0) {
            return new Result<>(201, "查询失败，未找到有效数据");
        }
        Result<PagingQueryResult<AddAwardedVo>> result = new Result<>(200, "查询成功");
        result.setData(addAwardedVoPage);
        return result;
    }

    @ApiOperation("导出校内比赛加分表中已公示数据")
    @PostMapping("/download")
    public Result<Object> downloadExcel(@ApiIgnore HttpServletResponse response,
                                        @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        //构建写入到Excel中的数据
        List<AddAwardedVo> allAnnounced = addawardedService.selectByAnnounced();
        System.out.println(allAnnounced);
        try {
            DownloadUtils.download(response, "校内比赛加分公示栏", allAnnounced, AddAwardedVo.class);
        } catch (IOException e) {
            return new Result<>(201, "表格导出错误，请联系管理人员");
        }

        return new Result<>(200, "表格导出成功");
    }

    @ApiOperation("新增校内比赛加分数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name="addAwardedVo",value="需要新增的校内比赛加分数据", required=true, paramType="body", dataType = "AddAwardedVo")
    })
    @PostMapping("/add")
    public Result<Boolean> addIntramuralCompetitionsPoints(@RequestBody AddAwardedVo addAwardedVo,
                                                           @ApiIgnore HttpSession session) {
        // 判断是否登录或者登录过期
        if (isAdminLogin(session)) {
            return new Result<>(203, "还未登录或登录过期，请重新登录");
        }
        Result<Boolean> result = new Result<Boolean>(201, "新增数据失败！");
        Boolean flag = addawardedService.addData(addAwardedVo);
        if (flag) {
            result.setResultCode(200);
            result.setMessage("新增数据成功");
        }
        result.setData(flag);
        return result;
    }
}
